{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Understanding Conda\n",
    "You should have conda and the ArcGIS API for Python installed on your system. If you don't, please see the [Install and set up guide](../install-and-set-up) for instructions. \n",
    "\n",
    "Let's talk about some basics of conda so you can manage installations and set up IDEs for projects and scripts. Whether installed manually or as part of another software installation (as is the case with ArcGIS Pro), conda can be installed in one of 2 varieties:\n",
    "* [Full Anaconda Distribution](https://www.anaconda.com/what-is-anaconda/) - installs conda and hundreds of additional Python packages\n",
    "* [Miniconda](https://conda.io/miniconda.html) - installs only Python, conda and other libraries needed to run conda\n",
    "\n",
    "\n",
    "See [Anaconda or Miniconda](https://conda.io/docs/user-guide/install/download.html#anaconda-or-miniconda) for an explanation of differences."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Understanding-Conda\" data-toc-modified-id=\"Understanding-Conda-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Understanding Conda</a></span><ul class=\"toc-item\"><li><span><a href=\"#The-Conda-Basics\" data-toc-modified-id=\"The-Conda-Basics-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>The Conda Basics</a></span></li><li><span><a href=\"#Conda-Environments\" data-toc-modified-id=\"Conda-Environments-1.2\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>Conda Environments</a></span><ul class=\"toc-item\"><li><span><a href=\"#ArcGIS-Pro\" data-toc-modified-id=\"ArcGIS-Pro-1.2.1\"><span class=\"toc-item-num\">1.2.1&nbsp;&nbsp;</span>ArcGIS Pro</a></span></li><li><span><a href=\"#Anaconda-Distribution\" data-toc-modified-id=\"Anaconda-Distribution-1.2.2\"><span class=\"toc-item-num\">1.2.2&nbsp;&nbsp;</span>Anaconda Distribution</a></span></li></ul></li><li><span><a href=\"#Activating-Environments\" data-toc-modified-id=\"Activating-Environments-1.3\"><span class=\"toc-item-num\">1.3&nbsp;&nbsp;</span>Activating Environments</a></span></li><li><span><a href=\"#Adding-Packages-to-Environments\" data-toc-modified-id=\"Adding-Packages-to-Environments-1.4\"><span class=\"toc-item-num\">1.4&nbsp;&nbsp;</span>Adding Packages to Environments</a></span></li><li><span><a href=\"#Setting-up-IDEs\" data-toc-modified-id=\"Setting-up-IDEs-1.5\"><span class=\"toc-item-num\">1.5&nbsp;&nbsp;</span>Setting up IDEs</a></span></li><li><span><a href=\"#Step-by-Step-on-macOS:-A-GIS-Environment\" data-toc-modified-id=\"Step-by-Step-on-macOS:-A-GIS-Environment-1.6\"><span class=\"toc-item-num\">1.6&nbsp;&nbsp;</span>Step by Step on macOS: A GIS Environment</a></span><ul class=\"toc-item\"><li><span><a href=\"#Create-an-environment:\" data-toc-modified-id=\"Create-an-environment:-1.6.1\"><span class=\"toc-item-num\">1.6.1&nbsp;&nbsp;</span>Create an environment:</a></span></li><li><span><a href=\"#Install-the-ArcGIS-API-for-Python¶\" data-toc-modified-id=\"Install-the-ArcGIS-API-for-Python¶-1.6.2\"><span class=\"toc-item-num\">1.6.2&nbsp;&nbsp;</span>Install the ArcGIS API for Python¶</a></span></li><li><span><a href=\"#Activate-the-environment\" data-toc-modified-id=\"Activate-the-environment-1.6.3\"><span class=\"toc-item-num\">1.6.3&nbsp;&nbsp;</span>Activate the environment</a></span></li><li><span><a href=\"#Start-the-Jupyter-Notebook\" data-toc-modified-id=\"Start-the-Jupyter-Notebook-1.6.4\"><span class=\"toc-item-num\">1.6.4&nbsp;&nbsp;</span>Start the Jupyter Notebook</a></span></li></ul></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Conda Basics\n",
    "The easiest way to understand conda is by working through the [30-minute Getting Started](https://conda.io/docs/user-guide/getting-started.html) tutorial. The sections below summarize and outline the commands you need to know to work with the ArcGIS API for Python.\n",
    "\n",
    "Conda is a platform-independent [package](https://docs.conda.io/projects/conda/en/latest/glossary.html#packages) manager application that can install, update, and remove Python packages. Packages are bundles of software and supporting files stored in any of a variety of repositories called [channels](https://docs.conda.io/projects/conda/en/latest/glossary.html#channels). Channels may be a default set of cloud locations, such as Anaconda Cloud, or private repositories created by individuals or organizations. Conda searches a default set of channels when listing or installing packages. See [Conda channels](https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/channels.html?highlight=channels#) for more information, such as how to configure conda to search specific channels. \n",
    "\n",
    "Esri has created a conda repository at a channel named `esri` to store the `arcgis` package. You can access channels with a flag in the conda install command, and instruct conda to install packages into specific conda [environments](https://docs.conda.io/projects/conda/en/latest/glossary.html#conda-environment). The rest of this document provides basic instructions to work with conda environments and links to resources providing more details.  \n",
    "## Conda Environments\n",
    "When you create conda environments, you create physical directories that isolate the specific Python interpreter and packages you install into the environment. This allows you to create multiple environments that can have different versions of software, including Python. You can easily create new environments and then switch between them without affecting other environments.  For detailed explanation and instructions, see the conda documentation on [Managing environments](https://conda.io/docs/user-guide/tasks/manage-environments.html).\n",
    "\n",
    "Let's explore what is available directly after installing conda through either [ArcGIS Pro](#ArcGIS-Pro) or the [Anaconda Distribution](#Anaconda-distribution)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ArcGIS Pro\n",
    "After the installation of ArcGIS Pro, the bundled conda will have 2 conda environments on your system:\n",
    "* `base`\n",
    " \n",
    "This environment contains an installation of Miniconda, a smaller version of Anaconda that includes conda, Python, the packages they depend on, as well as a select set of useful packages. \n",
    "\n",
    "* `arcgispro-py3`\n",
    "\n",
    "This environment contains Python and the arcgispro package, including all the [ArcPy](http://pro.arcgis.com/en/pro-app/arcpy/get-started/what-is-arcpy-.htm) functionality.\n",
    "\n",
    "<b>Manage conda environments with ArcGIS Pro:</b>\n",
    "\n",
    "You can use 3 different methods to maintain your conda environments. Each provides methods to install, update and remove packages:\n",
    "\n",
    "- [Python Package Manager](http://pro.arcgis.com/en/pro-app/arcpy/get-started/what-is-conda.htm)\n",
    "\n",
    "A GUI tool accessible from the ArcGIS Pro Project tab.\n",
    "\n",
    "- [Python Command Prompt](http://pro.arcgis.com/en/pro-app/arcpy/get-started/installing-python-for-arcgis-pro.htm#ESRI_SECTION2_7FDA7FD13D724C51B442D8859F3A25A8)\n",
    "\n",
    "A command-line shortcut that opens in the arcgispro-py3 directory and activates the environment. You can open this by searching <strong>Python Command Prompt</strong> in the windows start menu.\n",
    "\n",
    "- [Windows Command-Line](https://technet.microsoft.com/en-us/library/cc754340)\n",
    "\n",
    "The standard MS-DOS Windows command-line application.\n",
    "\n",
    "\n",
    "NOTE: For additional technical information about conda and ArcGIS Pro, see these articles:\n",
    "\n",
    "  * [Python and ArcGIS Pro 1.3: Conda](https://community.esri.com/docs/DOC-8359)\n",
    "  * [Conda and ArcGIS Pro 1.4](https://arcpy.wordpress.com/2016/10/21/conda-and-arcgis-pro/)\n",
    "  * [ArcGIS Notebooks in ArcGIS Pro (2.9) FAQ](https://community.esri.com/t5/python-documents/arcgis-notebooks-in-arcgis-pro-2-9-faq/ta-p/1116141)\n",
    "  * [ArcGIS Notebooks in ArcGIS Pro (3.0) FAQ](https://community.esri.com/t5/python-documents/arcgis-notebooks-in-arcgis-pro-3-0-faq/ta-p/1196582)\n",
    "\n",
    "### Anaconda Distribution\n",
    "After the installation, Anaconda installs Python, conda, and hundreds of default packages into one environment:\n",
    "* `base`\n",
    "\n",
    "<b>Manage conda environments with Anaconda:</b>\n",
    "\n",
    "You can use a couple different applications to manage your conda environments:\n",
    "\n",
    "- Terminal Application\n",
    "\n",
    "Standard command-line terminal applications on either the Windows, macOS, or Linux operating systems.\n",
    " \n",
    "- [Anaconda Navigator](https://docs.anaconda.com/anaconda/navigator/)\n",
    "\n",
    "A GUI application to comprehensively maintain conda enviroments."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Activating Environments\n",
    "There can be multiple environments in conda. To use an environment, you must activate it. The `activate` command switches between environments at a terminal application. See [Activating an environment](https://conda.io/docs/user-guide/tasks/manage-environments.html#activating-an-environment) for more information:\n",
    " * **Windows**:\n",
    " \n",
    "    `activate <environment_name>`\n",
    "<p></p>    \n",
    " * **macOS and Linux**: \n",
    " \n",
    "    `source activate <environment_name>`\n",
    "\n",
    "Once activated, type `python` in a terminal to access the Python interpreter for that environment instead of the global Python interpreter.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding Packages to Environments\n",
    "\n",
    "You can add a package to the currently active environment at the terminal with the following command:\n",
    "\n",
    "`conda install <package_name>`\n",
    "\n",
    "You can also specify specific environments and channels with the `--name` and `--channel` flags:\n",
    "\n",
    "`conda install --name <environment_name> --channel <channel_name> <package_name>`\n",
    "\n",
    "See [Managing packages](https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html#managing-packages) for detailed instructions on using the conda command-line interface. See the [The Default Environments](#The-Default-Environments:) section for detailed instructions on using ArcGIS Pro or Anaconda tools."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting up IDEs\n",
    "To begin projects in IDEs, you often have to specify the path to the Python interpreter. When using conda environments, each environment you create uses a different Python interpreter. \n",
    "Here there can be two cases:\n",
    "\n",
    "- If using the ArcGis Pro bundled conda environment, open Python Command Prompt by searching it in the start menu and use the below mentioned command.\n",
    "<p></p> \n",
    "\n",
    "- If you are using Anaconda then open Anaconda Command Prompt by searching it in start menu and use the below mentioned command.\n",
    "\n",
    "Type the following to get a list of all environments on your machine and the directory location of the specific Python interpreter (python.exe) used for that environment:\n",
    "\n",
    "`conda info --envs`\n",
    "\n",
    "![conda_info_envs Image](../../static/img/guide_getstarted_understandingconda01.png)\n",
    "\n",
    "Enter the directory name and add python.exe to set the correct path to the Python interpreter for a specific environment. For example, the correct interpreter for the arcgispro-py3 environment in an IDE based on the screenshot above would be:\n",
    "\n",
    "`C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\python.exe`\n",
    "\n",
    "![conda environment interpreter in PyCharm IDE](../../static/img/guide_getstarted_understandingconda_pycharm.png)\n",
    "\n",
    "See [Finding your Anaconda Python interpreter path](https://docs.anaconda.com/anaconda/user-guide/tasks/integration/python-path) for more detailed instructions on different systems.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step by Step on macOS: A GIS Environment\n",
    "Let's tie all these concepts together. The following steps create an environment, install the ArcGIS API for Python, activate the environment and start a Jupyter Notebook IDE from that environment:\n",
    "\n",
    "### Create an environment:\n",
    "```bash\n",
    "conda create --name gis-enviro\n",
    "```\n",
    "\n",
    "> You can name your environment anything you want.\n",
    "\n",
    "### Install the ArcGIS API for Python¶\n",
    "\n",
    "```bash\n",
    "conda install --name gis-enviro -c esri arcgis\n",
    "```\n",
    "> If you do not enter an environment name, the package will be installed in the active environment.\n",
    "\n",
    "### Activate the environment\n",
    "\n",
    "```bash\n",
    "source activate gis-enviro\n",
    "```\n",
    "The ArcGIS API for Python integrates with the Jupyter Notebook. [Jupyter Documentation](https://jupyter.org/) has more information about the suite of software and tools.\n",
    "\n",
    "### Start the Jupyter Notebook\n",
    "\n",
    "Navigate to the directory where you want to create notebooks (or where notebooks already exist).\n",
    "\n",
    "```bash\n",
    "jupyter notebook\n",
    "```\n",
    "If installed correctly, you'll see a message indicating that a web server has started. Your default web browser will launch from your current directory.\n",
    "Full documentation including samples and an API Reference located at https://developers.arcgis.com/python."
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": "255"
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
